package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.cst.SysConsts;
import com.itheima.dto.UserDTO;
import com.itheima.mapper.MyUserMapper;
import com.itheima.pojo.MyUser;
import com.itheima.service.MyUserService;
import com.itheima.service.PermissionService;
import com.itheima.support.PageResult;
import com.itheima.support.QueryPageBean;
import com.itheima.utils.MD5Util;
import com.itheima.utils.RedisUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author admin
 */
@DubboService
public class MyUserServiceImpl extends ServiceImpl<MyUserMapper, MyUser> implements MyUserService {

    @Resource
    private PermissionService permissionService;

    @Override
    @Transactional
    public boolean update(MyUser user) {
//        //保存用户表信息
//        this.saveOrUpdate(user);
//        //调用中间表删除中间表
//        checkGroupItemService.deleteByGroupId(checkGroupVO.getId());
//        //新增中间表的信息
//        checkGroupItemService.saveCheckGroupItem(checkGroupVO.getId(),checkGroupVO.getItems());
//        return true;
//        RedisUtil.set(SysConsts.REDIS_USER_PREFIX + user.getUserName(),queryUserDtoByUserName(user));
//        return this.updateById(user);
        return true;
    }

    @Override
    public boolean add(MyUser user) {
        String password = user.getPassword();
        user.setPassword(MD5Util.encode(password));
        RedisUtil.set(SysConsts.REDIS_USER_PREFIX + user.getUserName(),queryUserDtoByUserName(user));
        return this.save(user);
    }

    @Override
    public boolean delete(Long id) {
        return this.removeById(id);
    }

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {
        QueryWrapper<MyUser> wrapper = new QueryWrapper<>();
        String queryString = queryPageBean.getQueryString();
        if (queryString != null && !"".equals(queryString.trim())) {
            wrapper.like("user_name",queryString);
        }
        wrapper.orderByDesc("id");
        IPage<MyUser> iPage = this.page(new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),wrapper);
        return new PageResult(iPage.getTotal(), iPage.getRecords());
    }

    @Override
    public boolean updatePasswordByName(MyUser user) {
      QueryWrapper<MyUser>  queryWrapper= new QueryWrapper<>();
      queryWrapper.eq("user_name",user.getUserName());
        MyUser resultUser=this.getOne(queryWrapper);
        Long id = resultUser.getId();
        user.setId(id);
        String password = user.getPassword();
        user.setPassword(MD5Util.encode(password));
        RedisUtil.set(SysConsts.REDIS_USER_PREFIX + user.getUserName(),queryUserDtoByUserName(user));
       return this.updateById(user);
    }

    @Override
    public List<UserDTO> queryUser() {
        List<UserDTO> userDTOS = new ArrayList<>();
        list().forEach(user -> {
            userDTOS.add(queryUserDtoByUserName(user));
        });
        return userDTOS;
    }

    public UserDTO queryUserDtoByUserName(MyUser user) {
        UserDTO userDTO = new UserDTO();
        userDTO.setUserName(user.getUserName());
        userDTO.setPassword(user.getPassword());
        userDTO.setAuthority(permissionService.queryPermissionCodeByUserName(user.getUserName()));
        return userDTO;
    }
}